//
//  FogotPasswordViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-6-6.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import <UIImageView+AFNetworking.h>
#import "NetworkAccessManager.h"
#import "UIAlertView+Kirinrin.h"
#import "UtilHelper.h"

@interface ForgotPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *verfiycode;
@property (weak, nonatomic) IBOutlet UIImageView *verifycodeImage;

@end

@implementation ForgotPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self refreashVerifyCode:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backToUpAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)sendForgotPasswordAction:(id)sender {
    
    NSString *account = self.account.text;
    NSString *verifyCode = self.verfiycode.text;
    if (![UtilHelper isValidateMobile:account]) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildPhoneNumber];
        [self.account becomeFirstResponder];
        return ;
    }
    if (verifyCode.length == 0) {
        NSString *alertTitle = NSLocalizedString(@"错误", nil);
        NSString *alertMessage = NSLocalizedString(@"验证码不能为空!", nil);
        NSString *cancelButtonTitle = NSLocalizedString(@"确定", nil);
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    self.view.userInteractionEnabled = NO;
    
    NSString *uriString = [NSString stringWithFormat:@"Account/%@/forgotPassword.json",account];
    NSDictionary *parameters = @{@"verifyCode": verifyCode};
    
    [[NetworkAccessManager shareManager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation = %@ \n response = %@",operation, responseObject);
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        
        self.view.userInteractionEnabled = YES;

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [UIAlertView alertWithNetworkError];
        self.view.userInteractionEnabled = YES;
    }];
}

- (IBAction)refreashVerifyCode:(id)sender {
    NSLog(@"refreashVerifyCode");
    NSString *uriString = @"VerifyCode.image";
    //    [self.verifycodeImage setImageWithURL: [NSURL URLWithString:@"http://ibaby365.net/restservice/iBaby365API/V2/VerifyCode.image"]];
    NSMutableString *str =[NSMutableString stringWithString: kBaseUrl];
    [str replaceCharactersInRange:[str rangeOfString:@"V3"] withString:@"V2"];
    NSLog(@"str--%@",str);
//    NSString *verifyImageString = [NSString stringWithFormat:@"%@%@", kBaseUrl, uriString];
    NSString *verifyImageString = [NSString stringWithFormat:@"%@%@", str, uriString];
    NSLog(@"verifyImageString--%@",verifyImageString);
    NSURL *verifyImageURL = [NSURL URLWithString:verifyImageString];
    
    
    
    dispatch_queue_t queue = dispatch_queue_create("cn.com.suplus.ibaby.verify", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSData *data = [NSData dataWithContentsOfURL:verifyImageURL];
        
        dispatch_async( dispatch_get_main_queue(), ^{
            self.verifycodeImage.image = [[UIImage alloc]initWithData:data];
        });
    });
    
    
}


@end
